/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.client.param;

import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.edi.Constant;
import com.edi.db.table.ColumnCell;
import com.edi.db.table.RowEntity;
import com.edi.db.table.TableEntity;
import com.edi.exceptions.EdiParamException;
import com.edi.util.StringHelper;

public class EdiClientParam implements IClientParam {
	private static final Log log = LogFactory.getLog(EdiClientParam.class);

	protected String dsName;
	protected String methods;
	protected StringBuffer errorMessage;
	protected String state = Constant.EXEC_STATE_ERROR;
	protected Element ediPara;
	protected Element resultE;
	protected String serviceName;
	protected TableEntity returnTable;
	protected String newRow;
	//	protected List<ServiceParameter> serviceParamList=new ArrayList<ServiceParameter>();
	//	protected List<FixTable> fixTables;
	protected String pageSize; //每页记录数
	protected String totalSize; //总记录数，第一次为null， 以后回传到后台，不用再次查询数据库
	protected Integer totalPage; //每页记录数
	protected String transaction; //事务名

	protected EdiClientParam(Element ediPara) {
		this.errorMessage = new StringBuffer();
		this.dsName = ediPara.elementText("dsName");
		this.methods = ediPara.elementText("methods");
		this.serviceName = ediPara.elementText("serviceName");
		this.ediPara = ediPara;

		//构造返回客户端请求所需的element对象 
		resultE = DocumentHelper.createElement("result");
		resultE.addElement("state");
		resultE.addElement("dsName");
		resultE.addElement("errorInfo");
		resultE.addElement("table");
		resultE.addElement("object");
	}

	public EdiClientParam() {
	}

	public Element answerClinentParam() {
		log.debug(" resultE =null  " + (resultE == null));
		if (resultE != null)
			log.debug(" resultE =" + resultE.asXML());
		resultE.element("state").setText(state);
		resultE.element("dsName").setText(dsName);
		resultE.element("errorInfo").addCDATA(errorMessage.toString());
		if (StringUtils.isNotBlank(newRow)) {
			resultE.addElement("newRow");
			resultE.element("newRow").setText("1");
		}
		if (totalSize != null) {
			resultE.addElement("totalSize");
			resultE.element("totalSize").setText(totalSize.toString());
		}
		if (totalPage != null) {
			resultE.addElement("totalPage");
			resultE.element("totalPage").setText(totalPage.toString());
		}
		Element tableE = resultE.element("table");

		if (state.equals(Constant.EXEC_STATE_SUCESS)) { //执行成功			
			if (returnTable != null && returnTable.getRows().size() > 0) {
				while (returnTable.hasNextRow()) {
					Element rowE = tableE.addElement("row");
					RowEntity row = returnTable.next();
					Collection<ColumnCell> cells = row.getColumnCells()
							.values();
					for (ColumnCell cell : cells) {
						Element cellE = rowE.addElement(cell.getFieldName());
						cellE.addAttribute("fieldType", cell.getFieldType()
								.getName());
						if (cell.getFieldEntity() != null
								&& cell.getFieldEntity().isIdentify()) {
							cellE.addAttribute("primaryKey", "1");
						}
						if (cell.getFieldValue() != null) {
							if (cell.getFieldName().equals("tagdes"))
								log.debug(" cell.getFieldValue() ="
										+ cell.getFieldValue());
							//							cellE.addText(cell.getFieldValue().toString());
							cellE.addCDATA(Constant.CDATALEFT
									+ StringHelper.replaceCDATA(cell
											.getFieldType().getTypeValue(
													cell.getFieldValue()))
									+ Constant.CDATARIGHT);
						}
					}
				}
			}
		}

		return resultE;
	}

	public void excuteClientParam() throws EdiParamException {
		// TODO 子类实现

	}

	public Element getEdiPara() {
		return ediPara;
	}

	public void setEdiPara(Element ediPara) {
		this.ediPara = ediPara;
	}

	public String getDsName() {
		return dsName;
	}

	public String getMethods() {
		return methods;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getTransaction() {
		return this.transaction;
	}

	public void setTransaction(String transaction) {
		this.transaction = transaction;
	}
}
